package cn.jeeweb.web.modules.equipment.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.modules.equipment.entity.EqsMaintenanceCheckHistory;
import cn.jeeweb.web.modules.equipment.mapper.EqsMaintenanceCheckHistoryMapper;
import cn.jeeweb.web.modules.equipment.service.IEqsMaintenanceCheckHistoryService;
import cn.jeeweb.web.modules.sys.entity.FileResponse;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @description: eqs_maintenance_check服务接口
 */
@Transactional
@Service("eqsMaintenanceCheckHistoryService")
public class EqsMaintenanceCheckHistoryServiceImpl extends CommonServiceImpl<EqsMaintenanceCheckHistoryMapper, EqsMaintenanceCheckHistory> implements IEqsMaintenanceCheckHistoryService {

    public EqsMaintenanceCheckHistory getById(String json) {
        EqsMaintenanceCheckHistory result = super.getById(json);
        if (result != null) {
            List<FileResponse> defaultList = new ArrayList<FileResponse>();
            if (StringUtils.isNotBlank(result.getCheckAreaImage())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckAreaImage(), FileResponse.class);
                result.setCheckAreaImageArray(responses);
            } else {
                result.setCheckAreaImageArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckImportImage())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckImportImage(), FileResponse.class);
                result.setCheckImportImageArray(responses);
            } else {
                result.setCheckImportImageArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getUseToolFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getUseToolFile(), FileResponse.class);
                result.setUseToolFileArray(responses);
            } else {
                result.setUseToolFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckStandardFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckStandardFile(), FileResponse.class);
                result.setCheckStandardFileArray(responses);
            } else {
                result.setCheckStandardFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCheckMethodFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCheckMethodFile(), FileResponse.class);
                result.setCheckMethodFileArray(responses);
            } else {
                result.setCheckMethodFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getExistRiskFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getExistRiskFile(), FileResponse.class);
                result.setExistRiskFileArray(responses);
            } else {
                result.setExistRiskFileArray(defaultList);
            }

            if (StringUtils.isNotBlank(result.getCountermeasuresFile())) {
                List<FileResponse> responses = JSON.parseArray(result.getCountermeasuresFile(), FileResponse.class);
                result.setCountermeasuresFileArray(responses);
            } else {
                result.setCountermeasuresFileArray(defaultList);
            }
        }
        return result;
    }

    /**
     * 处理数组，转换为字符串
     *
     * @param list
     * @return
     */
    public String getResult_(List<FileResponse> list) {
        String result = "";
        if (list != null && list.size() > 0) {
            result = JSON.toJSONString(list);
        }
        return result;
    }

    /**
     * 初始化点检bean数据
     *
     * @param eqsMaintenanceCheckHistory
     * @return
     */
    public EqsMaintenanceCheckHistory initEqsMaintenanceCheckHistory_(EqsMaintenanceCheckHistory eqsMaintenanceCheckHistory) {
        List<FileResponse> areaImages = eqsMaintenanceCheckHistory.getCheckAreaImageArray();
        List<FileResponse> importImages = eqsMaintenanceCheckHistory.getCheckImportImageArray();
        List<FileResponse> useToolFiles = eqsMaintenanceCheckHistory.getUseToolFileArray();
        List<FileResponse> standardFiles = eqsMaintenanceCheckHistory.getCheckStandardFileArray();
        List<FileResponse> methodFiles = eqsMaintenanceCheckHistory.getCheckMethodFileArray();
        List<FileResponse> existRiskFiles = eqsMaintenanceCheckHistory.getExistRiskFileArray();
        List<FileResponse> countermeasuresFiles = eqsMaintenanceCheckHistory.getCountermeasuresFileArray();
        String areaImage = getResult_(areaImages);
        String importImage = getResult_(importImages);
        String useToolFile = getResult_(useToolFiles);
        String standardFile = getResult_(standardFiles);
        String methodFile = getResult_(methodFiles);
        String existRiskFile = getResult_(existRiskFiles);
        String countermeasuresFile = getResult_(countermeasuresFiles);

        eqsMaintenanceCheckHistory.setCheckAreaImage(areaImage);
        eqsMaintenanceCheckHistory.setCheckImportImage(importImage);
        eqsMaintenanceCheckHistory.setUseToolFile(useToolFile);
        eqsMaintenanceCheckHistory.setCheckStandardFile(standardFile);
        eqsMaintenanceCheckHistory.setCheckMethodFile(methodFile);
        eqsMaintenanceCheckHistory.setExistRiskFile(existRiskFile);
        eqsMaintenanceCheckHistory.setCountermeasuresFile(countermeasuresFile);
        return eqsMaintenanceCheckHistory;
    }
}